﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OfficialWeb.Application;

using OOS.Application.Dish.Dtos;
using OOS.Application.Dish.Services.Interfaces;
using OOS.Application.Setmeal.Dtos;
using OOS.Application.Setmeal.Services.Interfaces;
using OOS.Application.Setmeal.Vos;
using OOS.Application.User.Services.Interfaces;
using OOS.Core.RelyOn;
using OOS.Core.Setmeal;

namespace OOS.Application.Setmeal.Services
{
    /// <summary>
    /// 套餐管理
    /// </summary>
    public class SetmealBasicService:ISetmealBasicService,ITransient
    {
        private readonly ISqlSugarClient _client;
        private readonly IUserBasicService _userBasicService;
        private readonly ISetmealDishService _setmealDishService;
        private readonly IDishBasicService _dishBasicService;

        public SetmealBasicService(ISqlSugarClient client, IUserBasicService userBasicService, ISetmealDishService setmealDishService, IDishBasicService dishBasicService)
        {
            _client = client;
            _userBasicService = userBasicService;
            _setmealDishService = setmealDishService;
            _dishBasicService = dishBasicService;
        }

        /// <summary>
        /// 新增套餐
        /// </summary>
        /// <param name="setmealBasic"></param>
        /// <returns></returns>
        public Boolean InsertSetmealBasic(SetmealBasic setmealBasic)
        {
            if (setmealBasic == null) return false;
            //判断token中是否有用户数据
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            setmealBasic.Status = 1;
            setmealBasic.CreateUser = userId;
            setmealBasic.CreateDate = DateTime.Now;
            setmealBasic.UpdateUser = userId;
            setmealBasic.UpdateDate = DateTime.Now;
            setmealBasic.IsDeleted = false;
            int setmealId = _client.Insertable(setmealBasic).ExecuteReturnIdentity();
            //添加菜品套餐关系
            List<SetmealDish> setmealDishes = setmealBasic.setmealDishes;
            if (setmealDishes != null)
            {
                foreach (var setmealDish in setmealDishes)
                {
                    Boolean flag = _setmealDishService.InsertSetmealDish(setmealDish, setmealId);
                    if (!flag) return false;
                }
            }
            return setmealId > 0;
        }

        /// <summary>
        /// 根据id查询套餐详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SetmealBasicDto SelectSetmealById(int id)
        {
            if (id <= 0) return new SetmealBasicDto();
            SetmealBasic setmealBasic = SelectSetmealBasicById(id);
            if (setmealBasic == null) return new SetmealBasicDto();
            int CreateId = setmealBasic.CreateUser;
            int UpdateId = setmealBasic.UpdateUser;
            SetmealBasicDto setmealBasicDto = setmealBasic.Adapt<SetmealBasicDto>();
            setmealBasicDto.CreateUser = _userBasicService.SelectUserById(CreateId).Username;
            setmealBasicDto.UpdateUser = _userBasicService.SelectUserById(UpdateId).Username;
            List<SetmealDish> setmealDishes =  _setmealDishService.SelectSetmealDishBysetmealId(id);
            if (setmealDishes == null) return setmealBasicDto;
            List<DishAndNumDto> dishAndNumDtos = new List<DishAndNumDto>();
            DishAndNumDto dishAndNumDto = null;
            foreach (var setmealDish in setmealDishes)
            {
                dishAndNumDto = _dishBasicService.SelectDishById(setmealDish.DishId).Adapt<DishAndNumDto>();
                dishAndNumDto.DishNum = setmealDish.DishNum;
                dishAndNumDtos.Add(dishAndNumDto);
            }
            setmealBasicDto.dishAndNumDtos = dishAndNumDtos;
            return setmealBasicDto;
        }

        /// <summary>
        /// 根据条件(套餐名称模糊查询，套餐状态精确查询，套餐分类精确查询)分页查询
        /// </summary>
        /// <param name="searchSetmealVo"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageResponseDto<List<SetmealBasicDto>> SelectPageByConditions(SearchSetmealVo searchSetmealVo, int pageIndex, int pageSize)
        {
            int size = 0;
            List<SetmealBasicDto> setmealBasicDtos = _client.Queryable<SetmealBasic>()
                .Where(sb => sb.IsDeleted == false)
                .WhereIF(!String.IsNullOrEmpty(searchSetmealVo.SetmealName), sb => sb.SetmealName.Contains(searchSetmealVo.SetmealName))
                .WhereIF(searchSetmealVo.Status != 0, sb => sb.Status == searchSetmealVo.Status)
                .WhereIF(searchSetmealVo.CategoryId != 0, sb => sb.CategoryId == searchSetmealVo.CategoryId)
                .OrderBy(sb => sb.Sort, OrderByType.Desc)
                .OrderBy(sb => sb.CreateDate, OrderByType.Desc)
                .ToPageList(pageIndex, pageSize,ref size)
                .Adapt<List<SetmealBasicDto>>();
            for (int i = 0; i < setmealBasicDtos.Count;i++)
            {
                setmealBasicDtos[i] = this.SelectSetmealById(setmealBasicDtos[i].Id);
            }
            PageResponseDto<List<SetmealBasicDto>> pageResponseDto = new PageResponseDto<List<SetmealBasicDto>>();
            pageResponseDto.data = setmealBasicDtos;
            pageResponseDto.size = size;
            return pageResponseDto;
        }

        /// <summary>
        /// 根据id修改套餐信息
        /// </summary>
        /// <param name="setmealBasic"></param>
        /// <returns></returns>
        public bool UpdateSetmealBasicById(SetmealBasic setmealBasic)
        {
            if (setmealBasic == null) return false;
            SetmealBasic setmeal = SelectSetmealBasicById(setmealBasic.Id);
            if (setmeal == null) return false;
            //判断token中是否有用户数据
            int userId = 1; //JWTToken.GetUser().Id;
            //if (userId <= 0) { return false; }
            setmealBasic.CreateUser = setmeal.CreateUser;
            setmealBasic.CreateDate = setmeal.CreateDate;
            setmealBasic.UpdateUser = userId;
            setmealBasic.UpdateDate = DateTime.Now;
            //清空套餐菜品关系
            _setmealDishService.DeleteSetmealDishBysetmealId(setmealBasic.Id);
            //重新添加套餐菜品关系
            if (setmealBasic.setmealDishes != null)
            {
                setmealBasic.setmealDishes.ForEach(setmealDish =>
                {
                    _setmealDishService.InsertSetmealDish(setmealDish, setmealBasic.Id);
                });
            }
            return _client.Updateable(setmealBasic).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据id删除套餐信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteSetmealBasicById(int id)
        {
            if (id <= 0) return false;
            SetmealBasic setmealBasic = SelectSetmealBasicById(id);
            if (setmealBasic == null) return false;
            _setmealDishService.DeleteSetmealDishBysetmealId(id);
            setmealBasic.IsDeleted = true;
            return UpdateSetmealBasicById(setmealBasic);
        }

        /// <summary>
        /// 根据id修改套餐状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateSetmealStatusById(int id)
        {
            if (id <= 0) return false;
            SetmealBasic setmealBasic = SelectSetmealBasicById(id);
            if (setmealBasic == null) return false;
            //判断token中是否有用户数据
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            setmealBasic.Status = setmealBasic.Status == 1 ? 2 : 1;
            setmealBasic.UpdateUser = userId;
            setmealBasic.UpdateDate = DateTime.Now;
            return _client.Updateable(setmealBasic).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 批量修改套餐状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool UpdateSetmealStatusByIds(int[] ids, int type)
        {
            if (ids.Length <= 0 || ids == null) return false;
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            foreach (var id in ids)
            {
                SetmealBasic setmealBasic = SelectSetmealBasicById(id);
                setmealBasic.Status = type;
                setmealBasic.UpdateUser = userId;
                setmealBasic.UpdateDate = DateTime.Now;
                Boolean flag =  _client.Updateable(setmealBasic).ExecuteCommand() > 0;
                if (!flag) return false;
            }
            return true;
        }

        /// <summary>
        /// 批量删除套餐
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteSetmealBasicByIds(int[] ids)
        {
            if (ids.Length <= 0 || ids == null) return false;
            foreach (var id in ids)
            {
                Boolean flag =  this.DeleteSetmealBasicById(id);
                if (!flag) return false;
            }
            return true;
        }

        /// <summary>
        /// 根据id查询套餐数据库原始数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private SetmealBasic SelectSetmealBasicById(int id)
        {
            return _client.Queryable<SetmealBasic>()
                .Where(sb => sb.IsDeleted == false)
                .Where(sb => sb.Id == id)
                .Single();
        }
    }
}
